﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class FangSanInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        DropDownList IsDropList;

        protected override void OnLoad(EventArgs e)
        {

            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_PROLOUPAN";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["id"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
            }
            Persistence1.BindControl = panel1;
            BindData();
            IsLookPerson();
        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btnSave = (Button)FindControl("btnSave");
                        btnSave.Visible = false;
                    }
                }
            }
        }



        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            TextBox txtPID = (TextBox)FindControl("txtPID");
            TextBox txtZSPID = (TextBox)FindControl("txtZSPID");
            TextBox 产权人 = (TextBox)FindControl("产权人");
            TextBox 承租人 = (TextBox)FindControl("承租人");
            string strpid = HttpContext.Current.Request["PID"];
            if (string.IsNullOrEmpty(txtPID.Text)) { txtPID.Text = strpid; }
            string strZspersonid = txtZSPID.Text;
            if (!string.IsNullOrEmpty(strZspersonid))
            {
                EluZSProvider.ZSPERSON zsperson = _EluZSDataProvider.GetZSPERSON(strZspersonid);
                if (string.IsNullOrEmpty(产权人.Text)) { 产权人.Text = zsperson.产权人; }
                if (string.IsNullOrEmpty(承租人.Text)) { 承租人.Text = zsperson.承租人; }
            }
        }
        protected void BindData()
        {
            Button btnSave = (Button)FindControl("btnSave");
            Button btnReturn = (Button)FindControl("btnReturn");
            Button btnQKXY = (Button)FindControl("btnQKXY");
            IsDropList = (DropDownList)FindControl("IsDropList");
            TextBox txt楼盘类型 = (TextBox)FindControl("txt楼盘类型");
            IsDropList.SelectedIndexChanged += new EventHandler(IsDropList_SelectedIndexChanged);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnReturn.Click += new EventHandler(btnReturn_Click);
            btnQKXY.Click += new EventHandler(btnQKXY_Click);
            TextBox 产权人 = (TextBox)FindControl("产权人");
            TextBox 承租人 = (TextBox)FindControl("承租人");
            string strFId = HttpContext.Current.Request["ID"];
            string strQuery = HttpContext.Current.Request["QUERY"];
            if (!string.IsNullOrEmpty(strFId))
            {
                EluZSProvider.ProLoupan proloupan = _EluZSDataProvider.GetProLoupanRoom(HttpContext.Current.Request["ID"]);
                string strCZR = _EluZSDataProvider.GetSomeStr("zs_zsperson", "承租人", "id", proloupan.Zspid);
                string strBSW = _EluZSDataProvider.GetSomeStr("zs_cqzhxy", "提交备案", "fid", proloupan.Id);
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["QUERY"]))
                {
                    if (strQuery == "loupan")
                    {
                        btnQKXY.Visible = false;
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "showw", "<script>showw();</script>");
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(strCZR))
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "dipsaytr", "<script>dipsaytr();</script>");
                        }
                    }
                }
                if (strBSW == "1")//判断是否已备案
                {
                    产权人.Enabled = false;
                    承租人.Enabled = false;
                    btnQKXY.Text = "已签协议";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "displaycc", "<script>displaycc();</script>");
                }
            }
            else
            {
                if (strQuery == "loupan")
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "displayshow", "<script>displayshow();</script>");
                }
            }
        }

        void IsDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            TextBox txt楼层房间号 = (TextBox)FindControl("txt楼层房间号");
            if (IsDropList.SelectedValue == "待定")
            {
                txt楼层房间号.Text = "";
                txt楼层房间号.Visible = false;
            }
            else if (IsDropList.SelectedValue == "输入楼层房间号")
            {
                txt楼层房间号.Visible = true;
            }
        }

    

     
        protected void btnSave_Click(object sender, EventArgs e)
        {
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            //HttpContext.Current.Response.Redirect("FangSanInfo.aspx?ID=" + Persistence1.KeyValue + "&PID=" + HttpContext.Current.Request["PID"] + "&query=" + HttpContext.Current.Request["query"]);
            HttpContext.Current.Response.Redirect("FangSan.aspx?PID=" + HttpContext.Current.Request["PID"] + "&query=" + HttpContext.Current.Request["query"]);
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("FangSan.aspx?PID=" + HttpContext.Current.Request["PID"] + "&query=" + HttpContext.Current.Request["query"]);
        }
        protected void btnQKXY_Click(object sender, EventArgs e)
        {
            string pID = HttpContext.Current.Request["PID"];
            string fID = HttpContext.Current.Request["ID"];
            EluZSProvider.ProLoupan proloupan = _EluZSDataProvider.GetProLoupanRoom(HttpContext.Current.Request["ID"]);
            string temp = proloupan.楼层房间号;

            string qsid = _EluZSDataProvider.GetSomeStr("zs_cqzhxy", "id", "fid", fID);
            string getsession = _EluZSDataProvider.GetSomeStr("ZS_USER", "TABFS", "ID", userid);
            if (getsession == "0")
            {
                if (qsid != "")
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShu.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ID=" + qsid + "&FID=" + fID + "&action=edit&type=1");
                }
                else
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShu.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&FID=" + fID + "&action=insert&type=1");
                }
            }
            else if (getsession == "1")
            {
                if (qsid != "")
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShuInOne.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ID=" + qsid + "&FID=" + fID + "&action=edit&type=1");
                }
                else
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShuInOne.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&FID=" + fID + "&action=insert&type=1");
                }
            }
        }
    }
}


